<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class Network extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->model('network/network_model');
        $this->load->model('sim/sim_model');
        $this->load->library('pagination');
        $this->load->helper('url');
        $this->load->library('table');
        
    }
    
    public function index($page = '')
    {
        $this->mycommon->authAdmin();
        $data = array();
        $page = ($page != '') ? intval($page) : 1;
        $keyword = $this->input->get('k');
        $numPost = $this->network_model->count_all($keyword);
        $config['base_url'] = base_url().'admin/network/index'; // xác định trang phân trang
        $config["suffix"] = ".html?k=$keyword";
        $config['total_rows'] = $numPost; // xác định tổng số record
        $config['per_page'] = 10; // xác định số record ở mỗi trang
        $config['use_page_numbers'] = TRUE;
        $config['first_link'] = '«';
        $config['last_link'] = '»';
        $config['cur_tag_open'] = '<a href="#" class="active">';
        $config['cur_tag_close'] = '</a>';
        $config['num_links'] = 6;
        $config['uri_segment'] = 3; // xác định segment chứa page number
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $data['headTitle'] = 'Quản lý nhà cung cấp mạng';
        $data['current'] = 4;
        $data['keyword'] = $keyword;
        $this->load->library('Minifier');
        $data['msg'] = $this->input->get('msg');
        $data['group'] = $this->network_model->getAllNetwork($config['per_page'],$start, $keyword);
        $this->admin_layout->render('network/network_view', $data);
    }
    
    public function add()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $data['current'] = 4;
        if ($this->input->post('submit') && $this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_network','Tên nhóm', 'required|min_length[3]');
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            if ($this->form_validation->run() == TRUE) {
                $name = $this->input->post('name_network');
                if (! $this->network_model->networkExists($name)) {
                    if ($this->network_model->addNameNetwork($name)) {
                        redirect('admin/network/index');
                    }
                }
            }
        }
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('network/add_network_view', $data);
    }
    
 public function hide($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '') {
            show_404();
        }
        $data = array(
            'delete_flag' => '1'
        );
        $this->network_model->updateNameNetworkDeleteF($Id, $data);
        redirect('admin/network/index');
    }
    
    public function show($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
        $data = array(
            'delete_flag' => '0'
        );
        $this->network_model->updateNameNetworkDeleteF($Id, $data);
        redirect('admin/network/index');
    }
    
    public function delete($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
        if(!$this->network_model->checkNExists($Id)){
            $this->network_model->deleteNameNetwork($Id);
        } else {
            redirect('admin/network?msg=' . urlencode("Vẫn còn sim tồn tại, không thể xóa, vui lòng xóa hết sim trước!!!"));
            die;
        }
        redirect('admin/network/index');
    }
    
    public function edit($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
        $tmp = $this->network_model->getInfo($Id);
        if(!isset($tmp[0]))
            show_404 ();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $data['current'] = 4;
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_vn','Tên chỉ mục(tiếng việt)', 'required|min_length[3]');
            $this->form_validation->set_rules('name_en', 'Tên chỉ mục(tiếng anh)', 'required|min_length[3]');
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            $this->form_validation->set_message('checkUrl', '%s không chính xác');
            if ($this->form_validation->run() == TRUE) {
                $name_vn = $this->input->post('name_vn');
                $name_en = $this->input->post('name_en');
                $array = array(
                    'name'   =>  $name_en,
                );
                if ($this->network_model->updateNameNetwork($Id, $array)) {
                    redirect('admin/network/index');
                }
            }
        }
        $data['categoryInfo'] = $tmp[0];
        $data['headTitle'] = 'Sửa tên nhà mạng';
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('network/network_edit', $data);
    }

        public function updateNetworkPrice() {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $data['current'] = 4;
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_network',' Tên nhóm sim', 'required');
            $this->form_validation->set_rules('type_group',' Loại hình thức', 'required');
            $this->form_validation->set_rules('price_network_new', 'Giá nhóm sim mới', 'required');
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            if ($this->form_validation->run() == TRUE) {
                $name_network = $this->input->post('name_network');
                $price_network_new = $this->input->post('price_network_new');
                $price_network_new = str_replace(',', '', $price_network_new);
                $type_price_new = $this->input->post('type_price_new');
                $type_price_new = ($type_price_new == 1) ? 1 : 2;
                $price_network_new = intval($price_network_new);
                if($type_price_new == 1 && $price_network_new < 1000) {
                    $data['error'] = "Giá mới không được nhỏ hơn 1000đ";
                } elseif($type_price_new == 2 && ($price_network_new > 100 || $price_network_new < 0)) {
                    $data['error'] = "Giá thay đổi không chính xác";
                } else {
                    $typePirce = $this->input->post('type_group');
                    if($type_price_new == 1) {
                        $price_network_new = "`price_update` " . ($typePrice == 1 ? " + " : " - ") . $price_network_new;
                    } else {
                        $price_network_new = "`price_update` " . ($typePrice == 1 ? " + " : " - ") . "`price_update` * $price_network_new / 100";
                    }
                    $list_network_id = intval($name_network);
                    $priceStatus = ($typePrice == 1) ? 1 : -1;
                    $this->load->model('group/group_model');
                    if($this->network_model->checkNetworkExists($list_network_id)&& $price_network_new != 0){
                        if ($this->group_model->updatePriceNetwork('list_network_id', $list_network_id, $price_network_new) && $this->network_model->updateNameNetwork($list_network_id, array('priceStatus'  =>  $priceStatus))) {
                            redirect('admin/variation/index');
                        }
                    }
                }
            }
        }
        $data['tmp'] = $this->network_model->getFullNetwork();
        $data['headTitle'] = 'Sửa giá đại lý sim';
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('network/update_price_network_view', $data);
    }
}

?>
